package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.Material;
import com.sims.dao.IMaterialDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * Material entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.sims.bean.Material
 * @author MyEclipse Persistence Tools
 */

public class MaterialDAO extends HibernateDaoSupport implements IMaterialDAO {
	private static final Logger log = LoggerFactory
			.getLogger(MaterialDAO.class);
	// property constants
	public static final String AUTHOR = "author";
	public static final String CID = "cid";
	public static final String SCOPE = "scope";
	public static final String AUTHOR_ID = "authorId";
	public static final String AUTHOR_IDENTITY = "authorIdentity";
	public static final String STIME = "stime";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#save(com.sims.bean.Material)
	 */
	@Override
	public void save(Material transientInstance) {
		log.debug("saving Material instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#delete(com.sims.bean.Material)
	 */
	@Override
	public void delete(Material persistentInstance) {
		log.debug("deleting Material instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findById(java.lang.Integer)
	 */
	@Override
	public Material findById(java.lang.Integer id) {
		log.debug("getting Material instance with id: " + id);
		try {
			Material instance = (Material) getHibernateTemplate().get(
					"com.sims.bean.Material", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findByExample(com.sims.bean.Material)
	 */
	@Override
	public List findByExample(Material instance) {
		log.debug("finding Material instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Material instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Material as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findByAuthor(java.lang.Object)
	 */
	@Override
	public List findByAuthor(Object author) {
		return findByProperty(AUTHOR, author);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findByCid(java.lang.Object)
	 */
	@Override
	public List findByCid(Object cid) {
		return findByProperty(CID, cid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findByScope(java.lang.Object)
	 */
	@Override
	public List findByScope(Object scope) {
		return findByProperty(SCOPE, scope);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findByAuthorId(java.lang.Object)
	 */
	@Override
	public List findByAuthorId(Object authorId) {
		return findByProperty(AUTHOR_ID, authorId);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findByAuthorIdentity(java.lang.Object)
	 */
	@Override
	public List findByAuthorIdentity(Object authorIdentity) {
		return findByProperty(AUTHOR_IDENTITY, authorIdentity);
	}
	
	public List findByTime(Object stime) {
		return findByProperty(STIME, stime);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Material instances");
		try {
			String queryString = "from Material";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#merge(com.sims.bean.Material)
	 */
	@Override
	public Material merge(Material detachedInstance) {
		log.debug("merging Material instance");
		try {
			Material result = getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#attachDirty(com.sims.bean.Material)
	 */
	@Override
	public void attachDirty(Material instance) {
		log.debug("attaching dirty Material instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IMaterialDAO#attachClean(com.sims.bean.Material)
	 */
	@Override
	public void attachClean(Material instance) {
		log.debug("attaching clean Material instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IMaterialDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IMaterialDAO) ctx.getBean("MaterialDAO");
	}
}